﻿#region Using Statements
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.GamerServices;
using System.ComponentModel;
#endregion

namespace TunnelsOfDeath
{
    /// <summary>
    /// This is the main type for your game
    /// </summary>
    public partial class Game1 : Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        
        
        double MainTimer = 0;

        public Game1()
            : base()
        {
            graphics = new GraphicsDeviceManager(this);
            Content.RootDirectory = "Content";
            graphics.PreferredBackBufferWidth = 960;
            graphics.PreferredBackBufferHeight = 540;
            Globals.ScreenX = 960;
            Globals.ScreenY = 540;
        }

        /// <summary>
        /// Allows the game to perform any initialization it needs to before starting to run.
        /// This is where it can query for any required services and load any non-graphic
        /// related content.  Calling base.Initialize will enumerate through any components
        /// and initialize them as well.
        /// </summary>
        protected override void Initialize()
        {


            Globals.ScreenCleaner = new Texture2D(GraphicsDevice, 1, 1);
            Color pixel = new Color(68, 27, 11);
            Globals.ScreenCleaner.SetData<Color>(new Color[] {
                pixel
            });


            base.Initialize();
        }

        /// <summary>
        /// LoadContent will be called once per game and is the place to load
        /// all of your content.
        /// </summary>
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);
            Managers.TextureManager tm = new Managers.TextureManager();
            tm.LoadTexture(Content,"dirt");
            tm.LoadTexture(Content, "rock");
            tm.LoadTexture(Content, "wall");
            tm.LoadTexture(Content, "gold");
            tm.LoadTexture(Content, "player1");
            tm.LoadTexture(Content, "player2");
            tm.LoadTexture(Content, "explosion");
            tm.LoadTexture(Content, "enemy");
            tm.LoadTexture(Content, "door");
            tm.LoadTexture(Content, "backgr");

            Globals.Gui = new GUI(Content);
            //Globals.StateManager.ChangeState(GameState.startscreen);
            Globals.CurrentState = GameState.startscreen;

            //Screens
            Globals.Screens.Add(Content.Load<Texture2D>("start"));

            LoadSave.MapConverter conv = new LoadSave.MapConverter();
            conv.Convert();
        }


        /// <summary>
        /// UnloadContent will be called once per game and is the place to unload
        /// all content.
        /// </summary>
        protected override void UnloadContent()
        {
            // TODO: Unload any non ContentManager content here
        }

        protected override void Update(GameTime gameTime)
        {
            //System.Diagnostics.Debug.WriteLine(Globals.CurrentState.ToString());
            Globals.Gui.Update(gameTime);

            if (Globals.CurrentState == GameState.startup)
            {
                MainStateStartup(gameTime);
            }

            if (Globals.CurrentState == GameState.game)
            {
                MainStateGame(gameTime);
            }

            if (Globals.CurrentState == GameState.gameover)
            {
                MainStateGameOver(gameTime);
            }

            if (Globals.CurrentState == GameState.levelpassed)
            {
                MainStateLevelPassed(gameTime);
            }

            if (Globals.CurrentState == GameState.alllevelspassed)
            {
                MainAllLevelsPassed(gameTime);
            }

            if (Globals.CurrentState == GameState.levellost)
            {
                MainStateLevelLost(gameTime);
            }


            base.Update(gameTime);
        }


        protected override void Draw(GameTime gameTime)
        {
            spriteBatch.Begin();

            

            if (Globals.CurrentState == GameState.game || Globals.Gui.ShouldDrawMap)
            {
                DrawMapArea(gameTime);
            } 
            
            Globals.Gui.Draw(spriteBatch);

            spriteBatch.End();

            base.Draw(gameTime);
        }

    }
}
